package com.design.window;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;

public abstract class Route {
    private final String name;
    private final String href;
    private final JFrame frame;
    private final JPanel panel;
    private final int windowWidth;
    protected JFrame getFrame() {
        return frame;
    }
    protected JPanel getPanel() {
        return panel;
    }

    protected int getWindowWidth() {
        return windowWidth;
    }

    protected Route(String href, int width, int height, String name, String title){//生命周期  创建周期
        this.windowWidth = width;
        this.name = name;
        this.href = href;
        this.frame = new JFrame();
        this.frame.setSize(width,height);
        this.frame.setTitle(title);
        this.frame.setLocationRelativeTo(null);//居中
        this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.frame.setLayout(null);
        Route _this = this;
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if(_this.close()){
                    _this.frame.setVisible(false);
                }
            }
        });
        this.frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                _this.keyTyped(e.getKeyChar());
            }
            @Override
            public void keyPressed(KeyEvent e) {
                _this.keyPressed(e.getKeyChar());
            }
            @Override
            public void keyReleased(KeyEvent e) {
                _this.keyReleased(e.getKeyChar());
            }
        });
        this.panel = new JPanel(){
            @Override
            public void paint(Graphics g) {
                filling(g);
            }
        };
        this.panel.setSize(this.frame.getWidth(),this.frame.getHeight());
        this.frame.add(this.panel);
    }
    protected void refill(){
        this.panel.repaint();
    }
    protected void keyTyped(char key){}
    protected void keyPressed(char key){}
    protected void keyReleased(char key){}
    public String getName() {
        return name;
    }
    public String getHref(){
        return href;
    }
    public void setVisible(boolean visible){
        if(visible){
            if(!this.before()){
                return;
            }
            this.run();
        }
        this.frame.setVisible(visible);
    }
    protected void restart(){
        this.setVisible(false);
        Route _this = this;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                _this.setVisible(true);
                timer.cancel();
            }
        },1000);
    }
    abstract public boolean before();//生命周期 在页面显示之前确认是否显示页面
    abstract protected boolean close();//生命周期 是否关闭当前页面
    abstract protected  void run();//生命周期 页面将要显示时执行
    abstract protected void filling(Graphics g);//生命周期 绘制时执行
//    abstract protected
}
